<?php
/**
 * Created by PhpStorm.
 * User: 10838
 * Date: 2017/11/3
 * Time: 10:18
 */
namespace app\index\controller;
use app\admin\model\OpinionModel;
use app\admin\model\PrivilegeModel;
use app\admin\model\ViplevelModel;
use app\common\model\IntegralModel;
use app\common\model\ProjectModel;
use app\common\model\ReplyModel;
use app\index\model\UserModel;
use mg_api\Mg_api;
use think\Db;
use wechat\wechatCallbackapiTest;

class Index extends Common
{
    public function index()
    {

        $user = $this->user();
        //获取微信信息
        $this->get_wechat();
        $this->assign('user', $user);
        //获取项目状更新未查看
        $no_look_project = ProjectModel::where('is_look', '0')->where('user_id', $_SESSION['user_id'])->count();
        //积分未查看
        $no_look_integral = IntegralModel::where('is_look', '0')->where('user_id', $_SESSION['user_id'])->count();
        //回复未查看
        $opinions = OpinionModel::where('user_id', $_SESSION['user_id'])->select();
        $no_look_reply = [];

        foreach ($opinions as $k => $v) {
            $re = ReplyModel::where('opinion_id', $v->id)
                ->where('is_look', '0')
                ->select();
            if ($re) {
                $no_look_reply[] = $re;
            }
        }
        //总数
        $no_look_count = $no_look_project + $no_look_integral + count($no_look_reply);


        $this->assign('no_look_count', $no_look_count);

        return view('index');
    }

    //积分管理页面
    public function integral()
    {
        $user = $this->user();
        $this->get_wechat();
        //获取积分明细
        $data = $this->integral_details($user['openid']);
        $this->assign('intedral', $data['JData']);
        $this->assign('user', $user);
        return view('integral');
    }

    //会员权益
    public function power()
    {
        $id = input('param.viplevel');
        $re = ViplevelModel::find($id);
        $power = PrivilegeModel::all($re->privilege);
        $this->assign('power', $power);
        return view('power');
    }

    //按积分获取会员等级
    protected function viplevel($data)
    {
        $viplevel = ViplevelModel::all();
        foreach ($viplevel as $k => $v) {
            if ($data >= $v->range_up and $data < $v->range_down) {
                return $v;
            }
        }
    }

    protected function user()
    {
        $user = UserModel::find($_SESSION['user_id']);

        //查询总积分key
        if ($user['is_pass'] == '1') {
            $mg_api = new Mg_api();
            $data = [
                'key' => 'Get_LinkMan_Point',
                'wx_openid' => $user->openid
            ];
            $re = $mg_api->api($data);
            $re = json_decode($re, true);
            $user->jifen = $re['JData']['0']['point'];
            if ($user->jifen) {
                $re = $this->viplevel($user->jifen);
                //分配等级到模板
                $user["vip_title"] = $re['title'];
                $user['vip_id'] = $re['id'];
            } else {

                $user["vip_title"] = "暂无等级";
                $user['vip_id'] = 0;
                $user['jifens'] = $re['JData']['0']['point'];
            }
            return $user;
        } else {
            $this->error('账号未通过审核');
        }


    }

    public function get_wechat()
    {
        $wechat = new wechatCallbackapiTest();

        $getuser = $wechat->get_user($_SESSION['openid'], $_SESSION['access_token']);

        // var_dump($_SESSION['access_token']);
        // exit;
        $getuser = $wechat->get_user($_SESSION['openid'], $_SESSION['access_token']);
        // $getuser = $wechat->get_user('oqu6Qv0DM2PBtanzEZFjMHSq2iQ0','4_CA8g4bjQkUGiDyCHFcv4BP3Bk-68LR_S8gerfKfYz6FvYQz3bFYl-Jd4XfA7AUiW5sdQNKnAUSfb6J8EN2lHkg');

        //获取微信信息

        $this->assign('wechat_user', $getuser);
    }

    //获取积分明细
    protected function integral_details($openid)
    {
        $mg_api = new Mg_api();
        $data = [
            'key' => 'Get_LinkMan_PointList',
            'wx_openid' => $openid
        ];
        $re = $mg_api->api($data);
        $re = json_decode($re, true);
        return $re;
    }
    //获取页面业务联系电话
    public function business_tel(){
     echo json_encode(18582596534);
    }

}